home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-03 / qbasicpg.zip / FLPT.BAS < prev    next >
BASIC Source File  |  1987-09-11  |  2KB  |  74 lines

  1. '
  2. ' FLPT.BAS
  3. '
  4. ' Displays how a given real value is stored in memory.
  5. '
  6. '
  7. DEFINT A-Z
  8. DECLARE FUNCTION MHex$ (X AS INTEGER)
  9. DIM Bytes(3)
  10.  
  11. CLS
  12. PRINT "Internal format of IEEE number (all values in hexadecimal)"
  13. PRINT
  14. DO
  15.  
  16.    ' Get the value and calculate the address of the variable.
  17.    INPUT "Enter a real number (or END to quit): ", A$
  18.    IF UCASE$(A$) = "END" THEN EXIT DO
  19.    RealValue! = VAL(A$)
  20.    ' Convert the real value to a long without changing any of
  21.    ' the bits.
  22.    AsLong& = CVL(MKS$(RealValue!))
  23.    ' Make a string of hex digits, and add leading zeroes.
  24.    Strout$ = HEX$(AsLong&)
  25.    Strout$ = STRING$(8 - LEN(Strout$), "0") + Strout$
  26.  
  27.    ' Save the sign bit, and then eliminate it so it doesn't
  28.    ' affect breaking out the bytes
  29.    SignBit& = AsLong& AND &H80000000
  30.    AsLong& = AsLong& AND &H7FFFFFFF
  31.    ' Split the real value into four separate bytes
  32.    ' --the AND removes unwanted bits; dividing by 256 shifts
  33.    ' the value right 8 bit positions.
  34.    FOR I = 0 TO 3
  35.       Bytes(I) = AsLong& AND &HFF&
  36.       AsLong& = AsLong& \ 256&
  37.    NEXT I
  38.    ' Display how the value appears in memory.
  39.    PRINT
  40.    PRINT "Bytes in Memory"
  41.    PRINT " High    Low"
  42.    FOR I = 1 TO 7 STEP 2
  43.       PRINT " "; MID$(Strout$, I, 2);
  44.    NEXT I
  45.    PRINT : PRINT
  46.  
  47.    ' Set the value displayed for the sign bit.
  48.    Sign = ABS(SignBit& <> 0)
  49.  
  50.    ' The exponent is the right seven bits of byte 3 and the
  51.    ' leftmost bit of byte 2. Multiplying by 2 shifts left and
  52.    ' makes room for the additional bit from byte 2.
  53.    Exponent = Bytes(3) * 2 + Bytes(2) \ 128
  54.  
  55.    ' The first part of the mantissa is the right seven bits
  56.    ' of byte 2.  The OR operation makes sure the implied bit
  57.    ' is displayed by setting the leftmost bit.
  58.    Mant1 = (Bytes(2) OR &H80)
  59.    PRINT " Bit 31    Bits 30-23  Implied Bit & Bits 22-0"
  60.    PRINT "Sign Bit  Exponent Bits     Mantissa Bits"
  61.    PRINT TAB(4); Sign; TAB(17); MHex$(Exponent);
  62.    PRINT TAB(33); MHex$(Mant1); MHex$(Bytes(1)); MHex$(Bytes(0))
  63.    PRINT
  64.  
  65. LOOP
  66.  
  67. ' MHex$ makes sure we always get two hex digits.
  68. FUNCTION MHex$ (X AS INTEGER) STATIC
  69.    D$ = HEX$(X)
  70.    IF LEN(D$) < 2 THEN D$ = "0" + D$
  71.    MHex$ = D$
  72. END FUNCTION
  73.  
  74.